package com.webex.ta.hydra.gui.project;

import com.webex.ta.hydra.gui.component.ClosableTabbedPane;
import com.webex.ta.hydra.gui.component.CloseEventListener;
import com.webex.ta.hydra.gui.component.CloseEvent;
import com.webex.ta.hydra.gui.HydraEditorPane;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.action.CloseAction;

import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.awt.*;

/**
 * Author: Vegaz
 * Date: 2010-11-9
 * Time: 8:35:47
 */
public class ProjectTabbedPane extends ClosableTabbedPane implements ChangeListener, CloseEventListener {

    Map<HydraEditorPane, HydraProjectSuite> projects = new HashMap<HydraEditorPane, HydraProjectSuite>();

    public ProjectTabbedPane() {
        super();
        this.setVisible(false);
        this.addChangeListener(this);
        this.addCloseEventListener(this);
    }

    public void addProject(HydraProjectSuite project) {
        projects.put(project.getEditorPane(), project);
        this.add(project.getName(), project.getEditorPane());
        this.setSelectedComponent(project.getEditorPane());
        refresh();
    }

    public void updateProjectName(HydraProjectSuite project) {
        int index = this.indexOfComponent(project.getEditorPane());
        if (index >= 0) {
            this.setTitleAt(index, project.getName());
        }
    }

    public void activateProject(HydraProjectSuite project) {
        int index = this.indexOfComponent(project.getEditorPane());
        if (index >=0 ) {
            this.setSelectedIndex(index);
        }
    }

    public HydraProjectSuite getActiveProject() {
        HydraEditorPane activeEditor = (HydraEditorPane)this.getSelectedComponent();
        return projects.get(activeEditor);
    }



    public void removeProject(HydraProjectSuite project) {
        if (project == null) return;
        this.remove(project.getEditorPane());
        projects.remove(project.getEditorPane());
        refresh();
    }

    private void refresh() {
        this.setVisible(projects.size() > 0);
    }

    public Collection<HydraProjectSuite> getProjects() {
        return projects.values();
    }

    public void stateChanged(ChangeEvent e) {
        HydraProjectSuite activeProject = getActiveProject();
        GuiPackage gp = GuiPackage.getInstance();
        if (activeProject != null) {
            if (activeProject.getFile() != null)
                gp.setTestPlanFile(activeProject.getFile().getAbsolutePath());
            else
                gp.setTestPlanFile(activeProject.getName());
            gp.setHydraTable(getActiveProject().getEditorPane().getTable());
        } else {
            gp.setTestPlanFile(null);
        }
        gp.updateMenuAndToolBar();
    }

    public void onClosing(CloseEvent e) {
        int index = e.tabIndex;
        if (index >=0 ) {
            HydraEditorPane editorPane = (HydraEditorPane)this.getComponentAt(index);
            HydraProjectSuite project = projects.get(editorPane);
            CloseAction.closeProject(project);
        }
    }
}
